<template>

	<div id="reg">

		<el-row>
			<!-- 图标位置调整 -->
			<el-col :span="8" :offset="7">
				<el-row>
					<el-col :span="2" :offset="13">
						<i class="el-icon-food"></i>
					</el-col>
				</el-row>
				<!-- 表单控件区域 -->
				<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
					class="demo-ruleForm">
					<el-form-item label="用户名" prop="username">
						<el-input v-model="ruleForm.username" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="密码" prop="password">
						<el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="确认密码" prop="repass">
						<el-input type="password" v-model="ruleForm.repass" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item label="电话" prop="usertel">
						<el-input v-model="ruleForm.usertel" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item>
						<el-row>
							<el-col :span="12" :offset="6">
								<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
								<el-button @click="resetForm('ruleForm')">重置</el-button>
							</el-col>
						</el-row>
					</el-form-item>
				</el-form>
				<!-- 版权所有&跳转登录 -->
				<el-row>
					<el-col :span="18" :offset="6">
						<p>
							吃了吗点餐系统属于格莱科技旗下产品 copyright : raoqi@qq.com 最终解释权归学校所有。

							点击<a  v-on:click="jumpLogin">此处</a>去登陆。
						</p>
					</el-col>
				</el-row>
			</el-col>
		</el-row>
	</div>
</template>

<script>
	import axios from 'axios'
	export default {
		data() {
			var checkName = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('姓名不能为空'));
				}
				if (value.length <= 2) {
					return callback(new Error('姓名不得少于3个字符'));
				}
				callback();
			};
			var checkTel = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('电话不能为空'))
				}
				if (!(/^1[3456789]\d{9}$/.test(value))) {
					return callback(new Error('手机号码格式不正确'))
				}
				callback();
			};
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					if (this.ruleForm.repass !== '') {
						this.$refs.ruleForm.validateField('repass');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.ruleForm.password) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				ruleForm: {
					username: '',
					password: '',
					repass: '',
					usertel: ''
				},
				rules: {
					username: [{
						validator: checkName,
						trigger: 'blur'
					}],
					usertel: [{
						validator: checkTel,
						trigger: 'blur'
					}],
					password: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					repass: [{
						validator: validatePass2,
						trigger: 'blur'
					}],
				}
			};
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						// 验证成功，注册逻辑
						this.$http.post("/reg", {
							username: this.ruleForm.username,
							password: this.ruleForm.password,
							usertel: this.ruleForm.usertel
						}).then(res => {
							console.log(res);
							if (res.data.errCode == 0) {
								this.$message.success("注册成功");
								return;
							} else {
								this.$message.error(res.data.errMsg);
								return false;
							}
						}).catch(err => {
							this.$message.error("请求失败，请联系管理员");
						});


					} else {
						//验证失败，提示错误
						this.$message.error('输入有误，请更正后再试!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			},
			jumpLogin() {
				this.$router.push("/");
				return false;
			}
		}
	}
</script>

<style scoped>
	#reg {
		padding-top: 200px;
	}

	.el-icon-food {
		font-size: 48px;
		color: orange;
	}

	p {
		font-size: 12px;
	}
	a{
		color: red;
	}
</style>
